using NetProtocol.POD;
using System.Collections.Generic;

namespace IQIGame.Onigao.Logic
{
    /// <summary>
    /// 可拥有服务或buff等功能的关卡对象，Player，Hero，Entity(NPC)都属于此类
    /// </summary>
    public abstract class BaseLevelFunctionHost : BaseLevelEO
    {
        public abstract LevelRegion region { get; }
        public LevelBuffManager buffManager => _buffManager;

        protected LevelBuffManager _buffManager;

        public virtual void InitBuffs(IReadOnlyList<LevelBuffPOD> buffs)
        {
            //可能存在reset调用InitBuffs的情况，先清空
            ClearBuffMgr();
            if (buffs != null && buffs.Count > 0)
            {
                _buffManager = new LevelBuffManager(this);
                _buffManager.InitBuffs(buffs);
            }
        }

        /// <summary>
        /// 添加buff
        /// </summary>
        /// <param name="buffCid"></param>
        /// <param name="changedBuff">添加完成后发生改变的buff，如果添加成功该值为已添加的buff，失败如果有值则为堆叠层数+1的buff，以上都不满足为null</param>
        /// <param name="removeBuffCid">添加过程中因为被新buff覆盖而移除的buff，如果没有则为0</param>
        /// <returns>是否成功添加buff（只有buff列表+1才算）</returns>
        public bool AddBuff(int cid, out LevelNormalBuff changedBuff, out int removeBuffCid)
        {
            _buffManager = _buffManager ?? new LevelBuffManager(this);
            return _buffManager.AddBuff(cid, out changedBuff, out removeBuffCid);
        }

        public bool RemoveBuff(int cid)
        {
            if (_buffManager == null)
                return false;
            return _buffManager.RemoveBuff(cid);
        }

        public bool AddBuffStack(int cid, int addValue, out LevelNormalBuff changeSuccessBuff)
        {
            changeSuccessBuff = null;
            if (_buffManager == null)
                return false;
            return _buffManager.AddBuffStack(cid, addValue, out changeSuccessBuff);
        }

        public virtual bool TriggerBuffService(ServiceTriggerType triggerType)
        {
            if (_buffManager == null)
                return false;
            return _buffManager.TryTriggerService(triggerType);
        }

        private void ClearBuffMgr()
        {
            if (_buffManager != null)
            {
                _buffManager.Dispose();
                _buffManager = null;
            }
        }

        public override void OnReset()
        {
            ClearBuffMgr();
        }
    }
}
